package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    @Override
    public List<ProductModel> getProductList() {
        String sql = "select * from product";
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel product = null;
            try {
                while (rs.next()){
                    product = new ProductModel();
                    product.setId(rs.getInt("id"));
                    product.setName(rs.getString("name"));
                    product.setPhoto(rs.getString("photo"));
                    product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
                    product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
                    product.setPrice(rs.getBigDecimal("price"));
                    product.setCreateTime(rs.getString("create_time"));
                    product.setUpdateTime(rs.getString("update_time"));
                    product.setEnable(rs.getInt("enable"));
                    product.setNote(rs.getString("note"));
                    list.add(product);
                }
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        return list;
    }

    @Override
    public ProductModel getProduct(int id) {
        String sql = "select * from product where id = "+id;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        ProductModel product = null;
        try {
            if (rs.next()){
                product = new ProductModel();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPhoto(rs.getString("photo"));
                product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
                product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
                product.setPrice(rs.getBigDecimal("price"));
                product.setCreateTime(rs.getString("create_time"));
                product.setUpdateTime(rs.getString("update_time"));
                product.setEnable(rs.getInt("enable"));
                product.setNote(rs.getString("note"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public int addProduct(ProductModel productModel) {
        String sql = "insert into product(name,photo,user_type,product_type,price,create_time)" +
                " values('"+ productModel.getName()
                +"','" + productModel.getPhoto()
                +"'," + productModel.getUserType().getId()
                +"," + productModel.getProductType().getId()
                +"," + productModel.getPrice()
                +",'" + productModel.getCreateTime() + "')";
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteProduct(int id) {
        String sql = "delete from product where id="+id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public int updateProduct(ProductModel productModel) {
        String sql = "update product set" +
                " name='"+productModel.getName()
                +"',photo='"+productModel.getPhoto()
                +"',user_type="+productModel.getUserType().getId()
                +",product_type="+productModel.getProductType().getId()
                +",price="+productModel.getPrice()
                +",update_time='"+productModel.getUpdateTime()
                +"' where id="+productModel.getId();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductModel> searchProduct(String sql) {
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel product = null;
        try {
            while (rs.next()){
                product = new ProductModel();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPhoto(rs.getString("photo"));
                product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
                product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
                product.setPrice(rs.getBigDecimal("price"));
                product.setCreateTime(rs.getString("create_time"));
                product.setUpdateTime(rs.getString("update_time"));
                product.setEnable(rs.getInt("enable"));
                product.setNote(rs.getString("note"));
                list.add(product);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductModel> getProductList(int page, int size) {
        String sql = "select * from product limit "+ (page-1)*size +","+size;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel product = null;
        try {
            while (rs.next()){
                product = new ProductModel();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPhoto(rs.getString("photo"));
                product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
                product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
                product.setPrice(rs.getBigDecimal("price"));
                product.setCreateTime(rs.getString("create_time"));
                product.setUpdateTime(rs.getString("update_time"));
                product.setEnable(rs.getInt("enable"));
                product.setNote(rs.getString("note"));
                list.add(product);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductModel> getProductList(int page, int size, String sort) {
        String sql = "select * from product order by "+ sort +" limit "+ (page-1)*size +","+size;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        ProductModel product = null;
        try {
            while (rs.next()){
                product = new ProductModel();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPhoto(rs.getString("photo"));
                product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
                product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
                product.setPrice(rs.getBigDecimal("price"));
                product.setCreateTime(rs.getString("create_time"));
                product.setUpdateTime(rs.getString("update_time"));
                product.setEnable(rs.getInt("enable"));
                product.setNote(rs.getString("note"));
                list.add(product);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public long getProductCount() {
        String sql = "select count(*) from product";
        return JDBCUtils.getCount(sql);
    }
}
